package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.train.business.domain.Train_Carriage;
import com.example.train.business.domain.Train_CarriageExample;
import com.example.train.business.enums.SeatColEnum;
import com.example.train.business.mapper.Train_CarriageMapper;
import com.example.train.business.req.Train_CarriageQueryReq;
import com.example.train.business.req.Train_CarriageSaveReq;
import com.example.train.business.resp.Train_CarriageQueryResp;
import com.example.train.common.exception.BusinessException;
import com.example.train.common.exception.BusinessExceptionEnum;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Train_CarriageService {
    private static final Logger LOG = LoggerFactory.getLogger(Train_CarriageService.class);

    @Resource
    private Train_CarriageMapper train_CarriageMapper;

    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Train_CarriageSaveReq req) {
        DateTime now = DateTime.now();

        //自动计算列数和座位总数
        List<SeatColEnum> seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getColCount() * req.getRowCount());

        Train_Carriage train_Carriage = BeanUtil.copyProperties(req, Train_Carriage.class);
        if(ObjectUtil.isNull(train_Carriage.getId())){

            //检验存在性
            Train_Carriage trainCarriageDB=selectByIndex(train_Carriage.getIndex());
            if(ObjectUtil.isNotEmpty(trainCarriageDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
            }

            //在service中对以下三种数据赋值，因为请求参数不带这三个值，需要服务端自行设置
            train_Carriage.setId(SnowUtil.getSnowflakeNextId());
            train_Carriage.setCreateTime(now);
            train_Carriage.setUpdateTime(now);
            train_CarriageMapper.insert(train_Carriage);
        }else{
            train_Carriage.setUpdateTime(now);
            train_CarriageMapper.updateByPrimaryKey(train_Carriage);
        }
    }
    private  Train_Carriage selectByIndex(Integer index){
        Train_CarriageExample train_carriageExample=new Train_CarriageExample();
        train_carriageExample.createCriteria().andIndexEqualTo(index);
        List<Train_Carriage> list=train_CarriageMapper.selectByExample(train_carriageExample);
        if(ObjectUtil.isNotEmpty(list)){
            return list.get(0);
        }else{
            return null;
        }
    }
    public PageResp<Train_CarriageQueryResp> queryList(Train_CarriageQueryReq req) {
        Train_CarriageExample train_CarriageExample = new Train_CarriageExample();
        train_CarriageExample.setOrderByClause("train_code asc,id desc");
        Train_CarriageExample.Criteria criteria = train_CarriageExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Train_Carriage> train_CarriageList = train_CarriageMapper.selectByExample(train_CarriageExample);

        PageInfo<Train_Carriage> pageInfo = new PageInfo<>(train_CarriageList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Train_CarriageQueryResp> list = BeanUtil.copyToList(train_CarriageList, Train_CarriageQueryResp.class);

        PageResp<Train_CarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        train_CarriageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询车次的所有车厢信息
     * @param trainCode
     * @return
     */
    public List<Train_Carriage> selectByTrainCode(String trainCode) {
        Train_CarriageExample trainCarriageExample = new Train_CarriageExample();
        trainCarriageExample.setOrderByClause("`index` asc");
        Train_CarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return train_CarriageMapper.selectByExample(trainCarriageExample);
    }


}
